﻿<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="ACM.cmsarea08" %>
<script language="c#" runat="server">
void Page_Load(object o, EventArgs e ) {
        	try {
        	
            string q = "";
            string r = "";
            int l = 5;
            Response.ContentType = "application/json";
            
            if ( Request.Params["query"] == null) { 
            	/* Response.Write("null");*/ 
            	return; }
            else q = HttpUtility.UrlDecode( Request.Params["query"].ToString());
            if ( Request.Params["dynpart"] == null) { 
            	return; }
            else r = HttpUtility.UrlDecode( Request.Params["dynpart"].ToString());

            //if ( Request.Params["l"] != null) 
            //    Int32.TryParse( Request.Params["l"].ToString(),out l);
            //valore di default
            if (l == -1) l = 10;
            if (q.IndexOf("limit") == -1)
            {
                if (q.EndsWith(";")) q = q.Substring(0, q.IndexOf(";"));
                q = q + " limit " + l + ";";
            }
			
            object parametro = null;
            parametro =  Request.Form[r];
            if(parametro==null) {
					return; }
            parametro = parametro.ToString();
            q = q.Replace(r,parametro.ToString());

            
            
            DataSet ds = ACM.cmsarea08.DBManager.mainConnection.Helper_Dataset(q);
            if (ds == null) { Response.Write("['no set']");  return; }
            string reso = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string t = "";
                if (ds.Tables[0].Rows[i][0] is byte[])
                    t = System.Text.Encoding.UTF8.GetString((byte[])ds.Tables[0].Rows[i][0]);
                else
                    t = ds.Tables[0].Rows[i][0].ToString();
                reso += "\"" + t + "\",";
            }
            ds = null;
            if (reso.EndsWith(",")) reso = reso.Substring(0, reso.Length - 1);
            
             Response.Write("[" + reso + "]");
            
        	} catch (Exception ex) {

        		
        	}


}


</script>